package com.asura.ops.sync.client;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author: huyuntao(Mars)
 * @date: 2022/9/21
 * @description: 类的描述
 */
@Component
@Data
@ConfigurationProperties(prefix = "ops.target")
public class DataSourceConfig {

    private List<DataSourceProperties> dataSourceProperties;

    public List<DataSource> dataSourceList() {

        List<DataSource> dataSourceList = Lists.newArrayList();
        for (DataSourceProperties properties : dataSourceProperties) {
            DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
            dataSource.setDriverClassName(properties.getDriverClassName());
            dataSource.setUrl(properties.getUrl());
            dataSource.setUsername(properties.getUsername());
            dataSource.setPassword(properties.getPassword());
            dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效
            dataSource.setTestOnBorrow(false);
            dataSource.setTestOnReturn(false);
            dataSource.setTestWhileIdle(true);
            dataSource.setTimeBetweenEvictionRunsMillis(600000);
            dataSource.setMaxActive(20);
            dataSource.setInitialSize(10);

            dataSourceList.add(dataSource);
        }
        return dataSourceList;

    }
}
